/*******************************************************************************
* CHIPSEA F60 FLASH HAL
block size : 512 bytes
note: erase first before wite
version: released by Dylan 181229
*******************************************************************************/

#include "hal_cs_f60_flash.h"

#ifdef CS_F60

#include <stdio.h>


/******************************************************************************
@brief : Flash initialization
@param : NONE
*******************************************************************************/
void Flash_init(void)
{
	RCC_AHBPeriphClOCKCmd(RCC_AHBPeriph_ISP , ENABLE); //added by Eric.Chen 2015/05/09
	FMC_Init();
}

/*******************************************************************************
@brief : flash erase
@param : block      
	 offset   
	 size     
	 buf  
@ret   : 0 success     1 fail
@tips  : csa37fx60 512 Bytes/section   erase 512B each time
*******************************************************************************/
uint32_t flash_erase_sector(uint16_t sectorNo)
{
	uint8_t ret  = 0;

	uint32_t addr = (uint32_t)(sectorNo) << 9;

	if( FMC_ErasePage(addr) )   //error
	{
		ret = 1;
	}

	return ret;
}

////////////////////////////////////////////////////////////////////////////////////////////

/*******************************************************************************
@brief : flash read
@param : block   
	 offset   
	 size     
	 buf  
@ret   : (adr+sz) - OK, Failed Address -Fail
@tips  : csa37fx60 512 Bytes/section 
*******************************************************************************/
uint32_t hal_flash_read(uint16_t block, uint16_t offset, uint16_t size, uint8_t *buf)
{
	uint32_t ret ;

	uint32_t addr = ( (uint32_t)(block)<<9 )+ offset;

	ret = FMC_ReadPage(addr,size,buf);

	return ret;
}

/*******************************************************************************
@brief : flash write
@param : block   
	 offset   
	 size     
	 buf  
@ret   : 0 success     1 fail
@tips  : csa37fx60 512 Bytes/section 
*******************************************************************************/
uint32_t hal_flash_write(uint16_t block, uint16_t offset, uint16_t size, uint8_t *buf)
{
	uint32_t ret;

	uint32_t addr = ((uint32_t)(block) << 9) + offset ;

	ret = FMC_WritePage(addr,size,buf) ;

	return ret;
}

#endif /* End of CS_F60 */
